<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
//use app\admin\model\Blh as BlhModel;
use app\common\model\Blh as BlhModel;
use app\common\model\Allinpay as AllinpayModel;


class Supply extends Common{

    public function lst(){
        $list = Db::name('supply')->paginate(25);
        $count = Db::name('supply')->count();
        $page = $list->render();

        if(input('page')){
            $pnum = input('page');
        }else{
            $pnum = 1;
        }

        $this->assign(array(
            'list'=>$list,
            'count'=>$count,
            'page'=>$page,
            'pnum'=>$pnum,
        ));

        if(request()->isAjax()){
            return $this->fetch('ajaxpage');
        }else{
            return $this->fetch('lst');
        }
    }

    //修改状态
    public function gaibian(){
        $id = input('post.id');
        $name = input('post.name');
        $value = input('post.value');
        $data[$name] = $value;
        $data['id'] = $id;
        $count = Db::name('supply')->update($data);
        if($count > 0){
            ys_admin_logs('改变供应商开启或关闭状态','shops',$id);
            $result = 1;
        }else{
            $result = 0;
        }
        return $result;
    }



    public function info(){
        $id = input('supply_id');
            $supply = Db::name('supply')->where('id',$id)->find();
            if($supply){
                if(input('s')){
                    $this->assign('search',input('s'));
                }
                $this->assign('supply',$supply);
                return $this->fetch();
            }else{
                $this->error('参数错误');
            }
    }

    public function edit(){
        if(request()->isPost()){
            $post = input('post.');
            $admin_id = session('admin_id');
            $result = $this->validate($post,'Supply');

            if(true !== $result){
                //验证失败
                $value = array('status'=>0,'mess'=>$result);
            }else{
                $data=array(
                    'name'=>$post['name'],
                    'supply_desc'=>$post['supply_desc'],
                    'contacts'=>$post['contacts'],
                    'telephone'=>$post['telephone'],
                    'wxnum'=>$post['wxnum'],
                    'regname'=>$post['regname'],
                    'url'=>$post['url'],
                    'app_id'=>$post['app_id'],
                    'app_key'=>$post['app_key'],
                    'status'=>$post['status'],
                    'fenxiao'=>$post['fenxiao'],
                    'update_time'=>time(),

                );
                if(!empty($post['id'])){
                    //update
                    $data['id']=$post['id'];
                    $count = Db::name('supply')->update($data);
                }else{
                    $data['create_time']=time();
                    $count = Db::name('supply')->insert($data);
                }

                if($count !== false){
                    $value = array('status'=>1,'mess'=>'保存信息成功');
                }else{
                    $value = array('status'=>0,'mess'=>'保存信息失败');
                }

            }
            return json($value);
        }else{
            //todo::修改已有供应链操作
            if(input('supply_id')){
                $id = input('supply_id');

                $supply = Db::name('supply')->where('id',$id)->find();
                if($supply){
                    $this->assign('supply',$supply);
                    return $this->fetch();
                }else{
                    $this->error('参数错误');
                }
            }
            return $this->fetch();
        }

    }


    public function category(){
        if(input('sid')){
            $sid = input('sid');
            $supply = Db::name('supply')->where('id',$sid)->find();
            $catetable = 'supply_category_'.$supply['supply_code'];
            $list = Db::name($catetable)->select();
            $blh = new BlhModel();
            $res = $blh->getCategorys();
            if($res['result']=='true'){
                $list= $res['data'];
            }
            $this->assign('list',$list);
            return $this->fetch();
        }else{
            $this->error('供应链信息丢失');
        }

    }


    public function brand(){
        if(input('sid')){
            $sid = input('sid');
            $supply = Db::name('supply')->where('id',$sid)->find();
            $catetable = 'supply_brand_'.$supply['supply_code'];
            $list = Db::name($catetable)->select();
            $blh = new BlhModel();
            $res = $blh->getBrandList();
            if($res['result']=='true'){
                $list= $res['data'];
            }
            $this->assign('list',$list);
            return $this->fetch();
        }else{
            $this->error('供应链信息丢失');
        }

    }

    public function goods(){
        if(input('sid')){
            $sid = input('sid');
            $supply = Db::name('supply')->where('id',$sid)->find();
            $catetable = 'supply_goods_'.$supply['supply_code'];
            $list = Db::name($catetable)->field('itemId')->select();
            $old = array_column($list,'itemId');
            $blh = new BlhModel();
            $res = $blh->getGoodsId();
            if($res['result']=='true'){
                $list= $res['data'];
            }
            $ids = explode(',',$list['ids']);
            $update=array_diff($old,$ids);
            $del = array_diff($ids,$old);
            foreach($ids as $idk => $idv){
                $gres = $blh->getGoodInfo($idv);
                if($gres['result']==true){
                    $goodsInfo = $gres['data'];
                    $goodsInfo['product']['brand']=urldecode($goodsInfo['product']['brand']);
                    //TODO::urldecode
                    $goods = $goodsInfo['product'];
                    $goods['specifications']=json_encode($goods['specifications']);
                    Db::name($catetable)->insert($goods);
                }
            }
            $list = Db::name($catetable)->select();
            $this->assign('list',$list);
            return $this->fetch();
        }else{
            $this->error('供应链信息丢失');
        }

    }

    public function search(){
        if(input('post.keyword') != ''){
            cookie('supply_keyword',input('post.keyword'),7200);
        }else{
            cookie('supply_keyword',null);
        }
        $where = array();
        if(cookie('supply_keyword')){
            $where['name'] = cookie('supply_keyword');
            //$where['telephone'] = cookie('shops_keyword');
        }


        $list = Db::name('supply')->where($where)->order('create_time desc')->paginate(25);
        $count = Db::name('supply')->where($where)->order('create_time desc')->count();
        $page = $list->render();


        if(input('page')){
            $pnum = input('page');
        }else{
            $pnum = 1;
        }
        $search = 1;

        if(cookie('supply_keyword') != ''){
            $this->assign('keyword',cookie('supply_keyword'));
        }

        $this->assign('search',$search);
        $this->assign('pnum', $pnum);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $page);// 赋值分页输出
        $this->assign('count',$count);
        if(request()->isAjax()){
            return $this->fetch('ajaxpage');
        }else{
            return $this->fetch('lst');
        }
    }
}
